<?php
/**
 * ActiveRecord Finder.
 *
 * @package    XDao Framework
 * @author     Bogdan Stanescu <bstanescu@gmail.com>
 * @copyright  (c) 2011
 */
class XDao_List extends ArrayObject
{
	// returns the list items as value object
	public function vo()
	{
		$array = array();
		$iterator = $this->getIterator();
		while($iterator->valid())
		{
			$item = $iterator->current();
			if (is_array($item))
			{
				foreach($item as $key => $xdao_item)
				{
		    		$array[$iterator->key()][$key] = $xdao_item->vo;
				}
			}
			else
			{
		    	$array[$iterator->key()] = $item->vo;
			}
		    $iterator->next();
		}	
		return $array;
	}
	
	public function __get($value)
	{
		if ($value == 'vo')
		{
			return $this->vo();
		}
		else
		{
			$new_list = new self();
			$iterator = $this->getIterator();
			while($iterator->valid())
			{
				$item = $iterator->current();
				if (is_array($item) && isset($item[$value]))
				{
					$new_list[] = $item[$value];
				}
			    $iterator->next();
			}
			return $new_list;
		}
	}

	// // WIP:
	// // 
	// // apply same method on all elements in the list
	//
	// public function __call($method, $param)
	// {
	// 	$iterator = $this->getIterator();
	// 	while($iterator->valid())
	// 	{
	// 	    if (method_exists($iterator->current(), $method))
	// 		{
	// 			die();
	// 		}
	// 	    $iterator->next();
	// 		// print_r($iterator->current()); die;
	// 	}		
	// }

	/**
	 * Returns irst element in List
	 *
	 * @return XDao_Item (or extension of it)
	 * @author Bogdan Stanescu
	 */
	public function first()
	{
		$first = false;
		$iterator = $this->getIterator();
		if($iterator->valid())
		{
			$iterator->rewind();
			$first = $iterator->current();
		}
		return $first;
	}
	
}